package com.tourism.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.stereotype.Repository;

import com.tourism.shared.SysUtils;

@Repository
public class TransferDao {
	
	public int insertTransferShow(String title,String price,String status,
			String promotion,String picture) throws SQLException{
		PreparedStatement pstmt = null;
		Connection con = SysUtils.getMainDBCon();
		StringBuffer sql = new StringBuffer();
		ResultSet rs = null;
		int id = 0;
		
		sql.append("INSERT INTO transfer_show ( ");
		sql.append("  title,price,status,promotion,picture ");
		sql.append("  ) VALUES (?,?,?,?,?) ");

		try {
			pstmt = con.prepareStatement(sql.toString());
			pstmt.setString(1, title);
			pstmt.setFloat(2, Float.valueOf(price));
			pstmt.setInt(3, Integer.valueOf(status));
			pstmt.setInt(4, Integer.valueOf(promotion));
			pstmt.setString(5,picture);
			pstmt.executeUpdate();
			
			String getId = "select last_insert_id() as aa";
			pstmt = con.prepareStatement(getId);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				id = rs.getInt("aa");
			}
		} catch (SQLException e) {
			throw e;
		} finally {
			SysUtils.colseDBCon(con, pstmt, null);
		}
		return id;
	}

}
